<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/icon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/icon.png">
  <link rel="mask-icon" href="/blog/images/icon.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"bgape002.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="SSH SSH（secure shell）：SSH 为建立在应用层基础上的安全协议。SSH 是较可靠，专为远程登录会话和其他网络服务提供安全性的协议。 有效防止远程管理过程中的信息泄露 传输数据加密，防止DNS和IP欺骗 传输数据压缩，加快传输速度   OpenSSH是SSH协议的免费开源实现，Mac和Linux默认安装ssh客户端，windows 10和windows Server 2019（2">
<meta property="og:type" content="article">
<meta property="og:title" content="ssh基础使用">
<meta property="og:url" content="https://bgape002.gitee.io/2022/01/20/ssh%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="bgape002">
<meta property="og:description" content="SSH SSH（secure shell）：SSH 为建立在应用层基础上的安全协议。SSH 是较可靠，专为远程登录会话和其他网络服务提供安全性的协议。 有效防止远程管理过程中的信息泄露 传输数据加密，防止DNS和IP欺骗 传输数据压缩，加快传输速度   OpenSSH是SSH协议的免费开源实现，Mac和Linux默认安装ssh客户端，windows 10和windows Server 2019（2">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-01-20T01:18:58.000Z">
<meta property="article:modified_time" content="2022-01-20T05:02:13.168Z">
<meta property="article:author" content="bgape002">
<meta property="article:tag" content="ssh协议">
<meta property="article:tag" content="openSSH">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://bgape002.gitee.io/2022/01/20/ssh%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>ssh基础使用 | bgape002</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">bgape002</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">淡泊明志，宁静致远</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">66</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">27</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">61</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://bgape002.gitee.io/2022/01/20/ssh%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/head.png">
      <meta itemprop="name" content="bgape002">
      <meta itemprop="description" content="mail: bgape002@163.com">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="bgape002">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ssh基础使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2022-01-20 09:18:58 / 修改时间：13:02:13" itemprop="dateCreated datePublished" datetime="2022-01-20T09:18:58+08:00">2022-01-20</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blog/2022/01/20/ssh%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blog/2022/01/20/ssh%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="SSH"><a href="#SSH" class="headerlink" title="SSH"></a>SSH</h3><ul>
<li>SSH（<code>secure shell</code>）：SSH 为建立在应用层基础上的安全协议。SSH 是较可靠，专为远程登录会话和其他网络服务提供安全性的协议。<ul>
<li>有效防止远程管理过程中的信息泄露</li>
<li>传输数据加密，防止DNS和IP欺骗</li>
<li>传输数据压缩，加快传输速度</li>
</ul>
</li>
<li>OpenSSH是SSH协议的免费开源实现，Mac和Linux默认安装ssh客户端，windows 10和windows Server 2019（2018年后版本）默认添加OpenSSH协议。<a target="_blank" rel="noopener" href="https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_overview">windows中的OpenSSH</a></li>
</ul>
<span id="more"></span>

<h3 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h3><table>
<thead>
<tr>
<th>options</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>-p</td>
<td>指定ssh端口号，默认端口号22</td>
</tr>
<tr>
<td>-i</td>
<td>使用指定私钥文件链接服务器</td>
</tr>
</tbody></table>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#命令格式</span></span><br><span class="line">ssh [-options] [user@hostname]</span><br><span class="line"><span class="comment"># user 远程服务器登陆用户名，默认为当前用户</span></span><br><span class="line"><span class="comment"># hostname 远程服务器地址，可以是IP/域名/别名</span></span><br><span class="line"><span class="comment">## exit或logout命令可退出当前登陆;登陆密码为服务器上用户的账户密码</span></span><br><span class="line">ssh root@39.105.7.83</span><br></pre></td></tr></table></figure>

<h3 id="SSH配置（Linux）"><a href="#SSH配置（Linux）" class="headerlink" title="SSH配置（Linux）"></a>SSH配置（Linux）</h3><p>ssh配置信息保存在<code>~/.ssh</code></p>
<table>
<thead>
<tr>
<th>配置文件</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>known_hosts</td>
<td>客户端。记录曾连接服务器授权。ssh第一次连接一台服务器会有一个授权提示，确认授权后会记录在此文件中，下次连接服务器时无需重复授权</td>
</tr>
<tr>
<td>authorized_keys</td>
<td>服务端。客户端的免密连接公钥文件</td>
</tr>
<tr>
<td>config</td>
<td>客户端。记录连接服务器配置的别名</td>
</tr>
</tbody></table>
<ul>
<li><p><strong>服务器别名</strong></p>
<p>远程管理命令（<code>ssh, scp</code>等）连接一台服务器时，需要提供服务器地址、端口、用户名，可以把服务器连接参数打包记录到配置文件中，并为其设置一个简易的别名，方便访问服务器。</p>
<p>配置方法如下：</p>
<ul>
<li>创建/打开<code>~/.ssh/config</code>，在文件追加服务器配置信息</li>
</ul>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 服务器配置信息如下，HostName必须，其他项可省略</span></span><br><span class="line">Host broot</span><br><span class="line">	HostName 39.105.7.83</span><br><span class="line">	User root</span><br><span class="line">	Port 22</span><br></pre></td></tr></table></figure></li>
<li><p><strong>免密登陆</strong></p>
<ul>
<li>为避免每次登陆都需要输入密码，可以配置指定的加密算法验证密钥文件，配置免密登陆后，ssh和scp等远程管理命令都不再需要密码</li>
</ul>
<table>
<thead>
<tr>
<th>options</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>-t</td>
<td>指定加密类型，可选项：<code>[dsa,ecdsa,ed25519,rsa]</code>，默认为非对称加密（<code>rsa</code>）</td>
</tr>
<tr>
<td>-f</td>
<td>密钥文件名</td>
</tr>
<tr>
<td>-C</td>
<td>注释，将附加在密钥文件尾部</td>
</tr>
</tbody></table>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 命令格式</span></span><br><span class="line">ssh-keygen [-options]</span><br><span class="line"><span class="comment">########</span></span><br><span class="line"><span class="comment"># 客户端生成密钥对</span></span><br><span class="line">ssh-keygen</span><br><span class="line"><span class="comment"># 上传公钥到服务器 // 文件会自动上传为服务器特点文件 ~/.ssh/authorized_keys</span></span><br><span class="line">ssh-copy-id user@hostname</span><br></pre></td></tr></table></figure>

<ul>
<li><p>生成密钥时，若指定了文件名，连接服务器时需要通过<code>-i</code>指定要验证的密钥文件：<code>ssh -i file userAhostname</code>，默认文件名则可省略</p>
</li>
<li><p><strong>免密文件登陆</strong></p>
<p>出于安全考虑，大部分服务器提供商要求使用密钥文件进行远程登陆，如<strong>阿里云</strong>。下面以<strong>阿里云</strong>为例：</p>
<p><strong>A 生成密钥对</strong></p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen -t rsa -f ~/.ssh/[KEY_FILENAME] -C [USERNAME]</span><br><span class="line"><span class="built_in">chmod</span> 400 ~/.ssh/[KEY_FILENAME]</span><br></pre></td></tr></table></figure>

<p><strong>B 上传公钥</strong></p>
<p>登陆阿里云服务器<a target="_blank" rel="noopener" href="https://ecs.console.aliyun.com/?spm=5176.8789780.J_5834642020.2.c27b45b5zzfwWJ#/home">管理控制台</a>，网络与安全 -&gt; 密钥对 -&gt; 导入已有密钥</p>
<p><strong>C 连接阿里云</strong></p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh -i ~/.ssh/KEY_FILENAME [USERNAME]@[IP_ADDRESS]</span><br></pre></td></tr></table></figure>

<p><strong>D 简化登陆</strong></p>
<p>在配置文件中，使用<code>IdentityFile</code>指定私钥文件路径，可简化登陆连接命令。配置文件如下：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Host *</span><br><span class="line">	AddKeysToAgent <span class="built_in">yes</span></span><br><span class="line">	</span><br><span class="line">Host broot</span><br><span class="line">	HostName 39.105.7.83</span><br><span class="line">	User root</span><br><span class="line">	Port 22</span><br><span class="line">	IdentityFile ~/.ssh/id_rsa</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h3 id="ssh基本指令"><a href="#ssh基本指令" class="headerlink" title="ssh基本指令"></a>ssh基本指令</h3><p><strong>1，使用ssh连接远程主机</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">最简单的用法只需要指定用户名和主机名参数即可，主机名可以是 IP 地址或者域名。</span><br><span class="line"> </span><br><span class="line">$ ssh user@hostname</span><br></pre></td></tr></table></figure>

<p><strong>2，ssh连接到其他端口</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">SSH 默认连接到目标主机的 22 端口上，可以使用-p选项指定端口号</span><br><span class="line"> </span><br><span class="line">$ ssh -p 10022 user@hostname</span><br></pre></td></tr></table></figure>

<p><strong>3，使用ssh在远程主机执行一条<a target="_blank" rel="noopener" href="https://www.linuxcool.com/">命令</a>并显示到本地, 然后继续本地工作</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">直接连接并在后面加上要执行的命令就可以了</span><br><span class="line"> </span><br><span class="line">$ ssh pi@10.42.0.47 ls -l</span><br></pre></td></tr></table></figure>

<p><strong>4，在远程主机运行一个图形界面的程序</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">使用ssh的-X选项，然后主机就会开启 X11 转发功能</span><br><span class="line"> </span><br><span class="line">$ ssh -X feiyu@222.24.51.147</span><br></pre></td></tr></table></figure>

<p><strong>5，如何配置 SSH</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SSH 的配置文件在 /etc/ssh/sshd_config 中，你可以看到端口号, 空闲超时时间等配置项。</span><br></pre></td></tr></table></figure>

<p><strong>6，构建 ssh 密钥对</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">使用 ssh-keygen -t +算法 ，现在大多数都使用rsa或者dsa算法。</span><br><span class="line"> </span><br><span class="line">$ ssh-keygen -t rsa</span><br></pre></td></tr></table></figure>

<p><strong>7，查看是否已经添加了对应主机的密钥</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">使用-F选项</span><br><span class="line"> </span><br><span class="line">$ ssh-keygen -F 222.24.51.147</span><br></pre></td></tr></table></figure>

<p><strong>8，删除主机密钥</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">使用-R选项，也可以在~/.ssh/known_hosts文件中手动删除</span><br><span class="line"> </span><br><span class="line">$ ssh-keygen -R 222.24.51.147</span><br></pre></td></tr></table></figure>

<p><strong>9，绑定源地址</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">如果你的客户端有多于两个以上的 IP 地址，你就不可能分得清楚在使用哪一个 IP 连接到 SSH 服务器。为了解决这种情况，我们可以使用 -b 选项来指定一个IP 地址。这个 IP 将会被使用做建立连接的源地址。</span><br><span class="line"> </span><br><span class="line">$ ssh -b 192.168.0.200  root@192.168.0.103</span><br></pre></td></tr></table></figure>

<p><strong>10，对所有数据请求压缩</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">使用 -C 选项，所有通过 SSH 发送或接收的数据将会被压缩，并且任然是加密的。</span><br><span class="line"> </span><br><span class="line">$ ssh -C root@192.168.0.103</span><br></pre></td></tr></table></figure>

<p><strong>11，打开调试模式</strong></p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">因为某些原因，我们想要追踪调试我们建立的 SSH 连接情况。SSH 提供的 -v 选项参数正是为此而设的。其可以看到在哪个环节出了问题。</span><br><span class="line"> </span><br><span class="line">$ ssh -v root@192.168.0.103</span><br></pre></td></tr></table></figure>

<h3 id="etc-ssh-sshd-config配置文件详细说明"><a href="#etc-ssh-sshd-config配置文件详细说明" class="headerlink" title="/etc/ssh/sshd_config配置文件详细说明"></a><strong><code>/etc/ssh/sshd_config</code>配置文件详细说明</strong></h3><ul>
<li><code>ssh_config</code>：用于客户端的配置文件</li>
<li><code>sshd_config</code>：用于服务端的配置文件</li>
</ul>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">Port 2</span><br><span class="line">“Port”设置sshd监听的端口号。</span><br><span class="line"> </span><br><span class="line">ListenAddress 192.168.1.1</span><br><span class="line">“ListenAddress”设置sshd服务器绑定的IP地址。</span><br><span class="line"> </span><br><span class="line">HostKey /etc/ssh/ssh_host_key</span><br><span class="line"> </span><br><span class="line">“HostKey”设置包含计算机私人密匙的文件。</span><br><span class="line"> </span><br><span class="line">ServerKeyBits 1024</span><br><span class="line">“ServerKeyBits”定义服务器密匙的位数。</span><br><span class="line"> </span><br><span class="line">LoginGraceTime 600</span><br><span class="line">“LoginGraceTime”设置如果用户不能成功登录，在切断连接之前服务器需要等待的时间（以秒为单位）。</span><br><span class="line"> </span><br><span class="line">ClientAliveInterval 300（默认为0）</span><br><span class="line">这个参数的是意思是每5分钟，服务器向客户端发一个消息，用于保持连接</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">KeyRegenerationInterval 3600</span><br><span class="line">“KeyRegenerationInterval”设置在多少秒之后自动重新生成服务器的密匙（如果使用密匙）。重新生成密匙是为了防止用盗用的密匙解密被截获的信息。</span><br><span class="line"> </span><br><span class="line">PermitRootLogin no</span><br><span class="line">“PermitRootLogin”设置root能不能用ssh登录。这个选项一定不要设成“<span class="built_in">yes</span>”。</span><br><span class="line"> </span><br><span class="line">IgnoreRhosts <span class="built_in">yes</span></span><br><span class="line">“IgnoreRhosts”设置验证的时候是否使用“rhosts”和“shosts”文件。</span><br><span class="line"> </span><br><span class="line">IgnoreUserKnownHosts <span class="built_in">yes</span></span><br><span class="line">“IgnoreUserKnownHosts”设置ssh daemon是否在进行RhostsRSAAuthentication安全验证的时候忽略用户的“<span class="variable">$HOME</span>/.ssh/known_hosts”</span><br><span class="line"> </span><br><span class="line">StrictModes <span class="built_in">yes</span></span><br><span class="line">“StrictModes”设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权。这通常是必要的，因为新手经常会把自己的目录和文件设成任何人都有写权限。</span><br><span class="line"> </span><br><span class="line">X11Forwarding no</span><br><span class="line">“X11Forwarding”设置是否允许X11转发。</span><br><span class="line"> </span><br><span class="line">PrintMotd <span class="built_in">yes</span></span><br><span class="line">“PrintMotd”设置sshd是否在用户登录的时候显示“/etc/motd”中的信息。</span><br><span class="line"> </span><br><span class="line">SyslogFacility AUTH</span><br><span class="line">“SyslogFacility”设置在记录来自sshd的消息的时候，是否给出“facility pre”。</span><br><span class="line"> </span><br><span class="line">LogLevel INFO</span><br><span class="line">“LogLevel”设置记录sshd日志消息的层次。INFO是一个好的选择。查看sshd的man帮助页，已获取更多的信息。</span><br><span class="line"> </span><br><span class="line">RhostsAuthentication no</span><br><span class="line">“RhostsAuthentication”设置只用rhosts或“/etc/hosts.equiv”进行安全验证是否已经足够了。</span><br><span class="line"> </span><br><span class="line">RhostsRSAAuthentication no</span><br><span class="line">“RhostsRSA”设置是否允许用rhosts或“/etc/hosts.equiv”加上RSA进行安全验证。</span><br><span class="line"> </span><br><span class="line">RSAAuthentication <span class="built_in">yes</span></span><br><span class="line">“RSAAuthentication”设置是否允许只有RSA安全验证。</span><br><span class="line"> </span><br><span class="line">PasswordAuthentication <span class="built_in">yes</span></span><br><span class="line">“PasswordAuthentication”设置是否允许口令验证。</span><br><span class="line"> </span><br><span class="line">PermitEmptyPasswords no</span><br><span class="line">“PermitEmptyPasswords”设置是否允许用口令为空的帐号登录。</span><br></pre></td></tr></table></figure>









<h3 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h3><font size=2>

<p>[1] <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV15J411p7gU?spm_id_from=333.788.top_right_bar_window_default_collection.content.click">ssh小白教程…</a></p>
<p>[2] <a target="_blank" rel="noopener" href="https://baike.baidu.com/item/ssh/10407?fr=aladdin">ssh （安全外壳协议）</a></p>
<p>[3] <a target="_blank" rel="noopener" href="https://www.linuxprobe.com/ssh-use-11.html">SSH 命令的11种用法</a></p>
<p></font>&gt;</p>

    </div>

    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/ssh%E5%8D%8F%E8%AE%AE/" rel="tag"><i class="fa fa-tag"></i> ssh协议</a>
              <a href="/blog/tags/openSSH/" rel="tag"><i class="fa fa-tag"></i> openSSH</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2021/12/04/opencv/" rel="prev" title="opencv">
      <i class="fa fa-chevron-left"></i> opencv
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2022/01/24/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="next" title="设计模式">
      设计模式 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>


        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#SSH"><span class="nav-number">1.</span> <span class="nav-text">SSH</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">2.</span> <span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SSH%E9%85%8D%E7%BD%AE%EF%BC%88Linux%EF%BC%89"><span class="nav-number">3.</span> <span class="nav-text">SSH配置（Linux）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ssh%E5%9F%BA%E6%9C%AC%E6%8C%87%E4%BB%A4"><span class="nav-number">4.</span> <span class="nav-text">ssh基本指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#etc-ssh-sshd-config%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E"><span class="nav-number">5.</span> <span class="nav-text">&#x2F;etc&#x2F;ssh&#x2F;sshd_config配置文件详细说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5"><span class="nav-number">6.</span> <span class="nav-text">参考链接</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="bgape002"
      src="/blog/images/head.png">
  <p class="site-author-name" itemprop="name">bgape002</p>
  <div class="site-description" itemprop="description">mail: bgape002@163.com</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">61</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">66</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">bgape002</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'szhBf0Qamzsowubi1WnkXmUj-gzGzoHsz',
      appKey     : 'WpwxoK0fVJHvhzWwakr9vbpA',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
